<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn 2014年8月27日16:58:00
 * @小组应用
 */
namespace IKPHP\Apps\Group\Controllers;

use IKPHP\Apps\Group\Controllers;
use IKPHP\Common\Models\User;
use IKPHP\Apps\Group\Models\Group;
use IKPHP\Apps\Group\Models\GroupCate;
use IKPHP\Apps\Group\Models\GroupUsers;
use IKPHP\Apps\Group\Models\GroupTopics;

use IKPHP\Common\Models\LikeModel;
use IKPHP\Common\Models\Recommend;

class IndexController extends GroupBaseController
{
	private $userid = 0;
	
    public function initialize()
    {
        parent::initialize();
        // 访问者控制
		if ($this->visitor){
			$this->userid = $this->visitor['userid'];
		}        
    } 
    //小组首页  
    public function indexAction()
    {
 		if($this->visitor){
 			$this->_redirect('group/mine');
 		}else{
 			$this->_redirect('group/explore/topics');
 		}
    }
    //我的小组话题
    public function mineAction(){
		//访问控制
		if(!$this->visitor){return $this->_redirect('home/user/login');}  
		$userid = $this->userid;
		// 用户信息
		$strUser = User::getOneUser ( $userid );
		
		//我的小组话题
		$myGroup = GroupUsers::getGroupByUserid($userid); 
    		
		//我加入的所有小组的话题
		$arrTopic = $arrGroup = $myGroups = array();
		if(is_array($myGroup)){
			foreach($myGroup as $item){
				$arrGroup[] = $item['groupid'];
				$myGroups[] = Group::getOneGroup($item['groupid']);
			}
		}
    	$strGroup = implode(',',$arrGroup);
		
		if($strGroup){
			$arrTopics = GroupTopics::getTopics($strGroup,80);
			foreach($arrTopics as $key=>$item){
				$arrTopic[] = $item;
				$arrTopic[$key]['group'] = Group::getOneGroup($item['groupid']);
			}
		}
		$this->assign ( 'arrTopic', $arrTopic );   
		$this->assign ( 'myGroups', $myGroups );//我加入和管理的小组    	

		
    	//我常去的小组 加入的小组
		$myJoinGroup = Group::getUserJoinGroup( $userid );
		if(is_array($myJoinGroup)){
			foreach($myJoinGroup as $key=>$item){
				$arrMyGroup[] = Group::getOneGroup($item['groupid']);
			}
			
			$this->assign('arrMyGroup',$arrMyGroup);//我加入和管理的小组
		}
				
		
		$this->assign ( 'strUser', $strUser );    	
		$this->_config_seo ( array (
				'title' => '我的小组话题',
				'subtitle'=>'小组_'.C('ik_site_title'),
				'keywords' =>'',
				'description'=>'',
		) );    	
    }
	//小组页面
	public function showAction($id=''){
		if(!is_numeric($id) || empty($id)){
			return $this->error('呃...你想访问的页面不存在');
		}	
		$strGroup = Group::getOneGroup ( $id );
		if(empty($strGroup)) return $this->error ( '呃...你想要的东西不在这儿' );
		
		// 审核
		if ($strGroup ['isaudit'] == 1 && $strGroup ['userid'] != $this->userid) return $this->error ( '该小组正在审核中，请稍后访问！' );		
		
		
		//组长信息
		$strLeader = User::getOneUser ( $strGroup ['userid'] );		
		$this->assign ( 'strLeader', $strLeader );
		
		// 是否加入
		$isGroupUser = GroupUsers::isGroupUser ( $this->userid, $id );
		$this->assign ( 'isGroupUser', $isGroupUser );
		
		// 获取最近小组话题 40 条
		$arrTopics = GroupTopics::getNewTopic( $id, 40 );
		$this->assign ( 'arrTopics', $arrTopics );		
		// 获取最新加入会员
		$arrGroupUser = Group::getNewGroupUser ( $id, 12 );
		$this->assign ( 'arrGroupUser', $arrGroupUser );
		
		//最热话题 更多回应、喜欢和推荐时，最受大家喜爱的话题
		$type = $this->_get('type','trim','');
		if(!empty($type)){
			$arrEssenceTopics = GroupTopics::getEssenceTopics($id, 40);
			$this->assign ( 'arrEssenceTopics', $arrEssenceTopics );
		}
		$this->assign ( 'type', $type );
		
		$this->assign ( 'strGroup', $strGroup );
		$this->_config_seo ( array (
				'title' => $strGroup ['groupname'],
				'subtitle'=>'小组_'.C('ik_site_title'),
				'keywords' =>ikscws ( $strGroup ['groupname'] ),
				'description'=> sub_str ( clearText($strGroup ['groupdesc']), 200 ),
		)); 	
	}
	//rss 订阅
	public function rssAction($id=''){			
		if(!is_numeric($id) || empty($id)){
			return $this->error('呃...你想访问的页面不存在');
		}	
		$strGroup = Group::getOneGroup ( $id );
		if(empty($strGroup)) return $this->error ( '呃...你想要的东西不在这儿' );
		
		$arrTopics = GroupTopics::getTopics($id,60);
		$arrTopic = array();
		foreach($arrTopics as $key=>$item){
			$arrTopic[] = $item;
			$arrTopic[$key]['content'] = ikhtml_text($item['content']);
		}
		
		$pubdate = time();
		$this->assign('pubdate', $pubdate);
		$this->assign('arrTopic', $arrTopic); 
		$this->assign('strGroup', $strGroup);
		$this->assign('xmlheader','<?xml version="1.0" encoding="UTF-8" ?>');
        // 网页字符编码
        header('Content-Type:text/xml; charset=UTF-8');
        header('Cache-control: no-cache');  // 页面缓存控制
        header('X-Powered-By:IKPHP');		
		$this->view->setLayoutsDir(null);
	}
	//浏览小组成员
	public function membersAction($id=''){

		if(!is_numeric($id) || empty($id)){
			return $this->error('呃...你想访问的页面不存在');
		}	
		$strGroup = Group::getOneGroup ( $id );
		if(empty($strGroup)) return $this->error ( '呃...你想要的东西不在这儿' );
		
		$strGroup['user'] = User::getOneUser($strGroup['userid']);
		//喜欢 和 推荐 的帖子数
		$strGroup['topic']['count_like'] = LikeModel::countUserLikeByType($strGroup['userid'],'GroupTopics');
		$strGroup['topic']['count_recommend'] = Recommend::countUserRecByType($strGroup['userid'],'GroupTopics');
		
		//小组组长信息
		$leaderId = $strGroup['userid'];
		$strLeader = User::getOneUser($leaderId);
		$this->assign('strLeader',$strLeader);
			
		//管理员信息
		$arrAdmin =  GroupUsers::getAdmins($id); 
		$this->assign('arrAdmin',$arrAdmin);
		
		//小组会员
		$arrGroupUser = GroupUsers::getGroupUser($id); 
		$this->assign('arrGroupUser', $arrGroupUser);
		
		$this->assign('strGroup',$strGroup);
		$this->_config_seo ( array (
				'title' => $strGroup['groupname'].'的小组成员 ',
				'subtitle'=>'小组_'.C('ik_site_title'),
				'keywords' =>'',
				'description'=>'',
		) ); 		
		
	}	
}